const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function getSingleVariant(req, res, next) {
  logger.winston.info('variants.getSingleVariant');
  
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from synonym_variants where synonym_variant_id = ?",[req.params.synonym_variant_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }    
  
  
}

async function getSynonymVariants(req, res, next) {
  logger.winston.info('variants.getSynonymVariants');
  
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from synonym_variants where synonym_id = ? order by synonym_variant_id desc",[req.params.synonym_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }     
  

}

async function getSynonymsVariants(req, res, next) {
  logger.winston.info('variants.getSynonymVariants');
  const synonymsId = req.params.synonyms_id;
  var array_synonymIds = synonymsId.split(","); //Very hacky due to the node-sqlite not supporting IN from an array
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from synonym_variants where synonym_id in (" + array_synonymIds + ")");
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }    
  

}

async function createVariant(req, res, next) {
  logger.winston.info('variants.createVariant');

  
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into synonym_variants (synonym_id, synonym_value) values (?, ?)", [req.body.synonym_id, req.body.synonym_value]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated',synonym_id:rows.insertId });
			  }else{
			  	  res.status(500).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
	    
  
}

async function removeVariant(req, res, next) {
  logger.winston.info('variants.removeVariant');

  
    try {
    	
        const [rows,fields] = await db_promise.query("delete from synonym_variants where synonym_variant_id = ?", [req.body.synonym_variant_id]);
        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  } 
	    
  
  
}

async function removeSynonymVariants(req, res, next) {
  logger.winston.info('variants.removeSynonymVariants');
  
    try {
    	
        const [rows,fields] = await db_promise.query("delete from synonym_variants where synonym_id = ?", [req.body.synonym_id]);
        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }   
  

}

module.exports = {
  getSingleVariant,
  getSynonymVariants,
  createVariant,
  removeVariant,
  removeSynonymVariants,
  getSynonymsVariants
};
